package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    @Transactional
    public Integer add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);   //默认套餐状态改为停售,因为其内的菜品状态可能为停售
        Integer tag=setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        setmealDishList.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insert(setmealDishList);
        return tag;
    }

    @Override
    public PageResult findAll(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealVOList=setmealMapper.selectAll(setmealPageQueryDTO);
        Page<SetmealVO> page = (Page<SetmealVO>) setmealVOList;
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public Integer remove(Long[] ids) {
        //TODO 起售中的套餐无法删除

        //找出那些停售的套餐id,如果ids中含有启售的套餐,则提示客户端"启售中的套餐无法删除"
        ArrayList<Long> idList = setmealDishMapper.selectOpenSetmeal(ids);

        if(ids.length==idList.size()){
            //删除setmeal表中的相关记录
            Integer tag=setmealMapper.delete(idList);

            //删除setmeal_dish表中的相关记录
            setmealDishMapper.delete(idList);
            return tag;
        }else{
            return -1;  //提示客户端"启售中的套餐无法删除"
        }
    }

    @Override
    public SetmealVO findById(Long id) {
        return setmealMapper.selectById(id);
    }

    @Override
    @Transactional
    public Integer modify(SetmealDTO setmealDTO) {
        //修改套餐信息,操作setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        Integer tag=setmealMapper.update(setmeal);

        Long setmealId = setmeal.getId();
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(setmealId);
        //删除套餐和菜品的关联关系，操作setmeal_dish表
        setmealDishMapper.delete(longs);

        //重新插入套餐和菜品的关联关系，操作setmeal_dish表
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        setmealDishList.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insert(setmealDishList);
        return tag;
    }

    @Override
    public Integer setStatus(Long id, Integer status) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        if(status.equals(StatusConstant.ENABLE)){
            //套餐中含有停售的菜品时,该套餐无法启售
            List<Dish> closeDishList = setmealDishMapper.selectCloseDishesFromSetmeal(id);
            if(closeDishList.size()!=0){
                //说明套餐中含有停售的菜品
                return -1;
            }
        }
        return setmealMapper.update(setmeal);
    }
}